package samoloty.view;

import Utils.ScreensOrder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import javax.swing.border.Border;
import javax.swing.event.MouseInputListener;

/**
 *
 * @author borysfan
 */
public class MyButton extends JButton{

    public final static int ACTIVE_BUTTON = 1;
    public final static int UNACTIVE_BUTTON = 2;
    public final static int CHOOSE_BUTTON = 3;
    private JLabel label1 = null;
    private JLabel label2 = null;
    private MyButton instance;

    public MyButton() {
        super();
        initButton(90, 40);
        setAsActive();
        instance = this;
    }

    public MyButton(String name) {
        super(name);
        initButton(90, 40);
        setAsActive();
        instance = this;
    }

    public MyButton(String name, int width, int height) {
        super(name);
        initButton(width, height);
        setAsActive();
        instance = this;
    }

    public MyButton(String line, String line2) {
        super();
        initButton(90, 40);

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        label1 = new MyLabel(line);
        label2 = new MyLabel(line2);
        label1.setAlignmentX(CENTER_ALIGNMENT);
        label2.setAlignmentX(CENTER_ALIGNMENT);
        add(label1);
        add(label2);
        setAsActive();
        instance = this;
    }

    private void initActions(){
        addFocusListener(new ButtonFocus());
        addMouseListener(new MouseOver());
    }

    private void initButton(int width, int height) {
        this.setBounds(0, 0, width, height);
        this.setMinimumSize(new Dimension(width, height));
        this.setMaximumSize(new Dimension(width, height));
        this.setPreferredSize(new Dimension(width, height));
        this.initActions();
        this.setOpaque(true);
        this.setFont(new Font("Tahoma", 0, 16));
        this.setFocusable(true);
        this.registerKeyboardAction(this.getActionForKeyStroke(
                KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
                JComponent.WHEN_FOCUSED);

        this.registerKeyboardAction(this.getActionForKeyStroke(
                KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
                JComponent.WHEN_FOCUSED);
        setAlignmentX(Component.CENTER_ALIGNMENT);
    }

    public void setAsUnactive() {
        this.setBackground(new java.awt.Color(0, 0, 0));
        this.setForeground(new java.awt.Color(0, 145, 255));
        this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 145, 255)));
    }

    public void setAsActive() {

        this.setForeground(new java.awt.Color(200, 200, 200));
        this.setBackground(new java.awt.Color(90, 90, 90));       
        this.setBorder(null);
        this.setEnabled(true);
        if (label1 != null && label2 != null) {
            label1.setForeground(new java.awt.Color(200, 200, 200));
            label2.setForeground(new java.awt.Color(200, 200, 200));
        }
    }

    public void setAsChoose() {
        this.setForeground(new java.awt.Color(200, 200, 200));
        this.setBackground(new java.awt.Color(0, 66, 0));
        this.setBorder(null);
        this.setEnabled(true);
    }

    /* @Override
    public void setVisible(boolean value){
    visible = value;
    if(value==false){
    this.setForeground(MyColors.getBlack());
    this.setBackground(MyColors.getBlack());
    this.setBorder(null);

    if(label1!=null && label2!=null){
    label1.setForeground(MyColors.getBlack());
    label2.setForeground(MyColors.getBlack());
    }

    }else{
    setAsActive();
    }
    }*/
    public void setPosition(Point p) {
        this.setBounds(p.x, p.y, getWidth(), getHeight());
    }

    private class ButtonFocus implements FocusListener {

        public void focusGained(FocusEvent e) {
            instance.setBorder(BorderFactory.createLineBorder(Color.WHITE, 3));
            
        }

        public void focusLost(FocusEvent e) {
            instance.setBorder(null);
        }
    }


     private class MouseOver implements MouseListener{

            @Override
            public void mouseClicked(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                //JButton btn =(JButton)e.getComponent();
                instance.setBorder(BorderFactory.createLineBorder(Color.WHITE, 3));

            }

            @Override
            public void mouseExited(MouseEvent e) {
                if(!instance.isFocusOwner())
                    instance.setBorder(null);
            }

     }

}
